package com.example.transaction.sys.controller;

import javax.annotation.Resource;

import com.example.transaction.common.vo.Result;
import com.example.transaction.config.WebSocket;
import com.example.transaction.sys.entity.Chat;
import com.example.transaction.sys.entity.User;
import com.example.transaction.sys.service.IChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/sys/chat")
public class ChatController {

    @Autowired
    private IChatService chatService;


    /**
     * 获取所有跟当前userID聊天的用户信息
     * @param userID
     * @return
     */
    @GetMapping("/{userID}")
    public Result<List<User>> getChatUsers(@PathVariable Integer userID) {
        List<User> chatUsers = chatService.getChatUsers(userID);
        return Result.success(chatUsers);
    }

    @GetMapping("/{userId}/{targetUserId}")
    public Result<List<Chat>> getChatHistory(@PathVariable Integer userId, @PathVariable Integer targetUserId) {
        List<Chat> chatHistory = chatService.getChatHistory(userId, targetUserId);
        return Result.success(chatHistory);
    }


    @PostMapping
    public Result<Chat> addChatMessage(@RequestBody Chat chat) {
        Chat savedChat = chatService.saveChat(chat.getUserID(), chat.getTargetUserID(), chat.getUserName(), chat.getTargetUserName(), chat.getContent());
        return Result.success(savedChat);
    }
}


